<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="宠物名称" prop="recipeName">
        <el-input v-model="queryParams.recipeName" placeholder="请输入宠物名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="评论状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 180px">
          <el-option v-for="dict in statusOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column label="评论ID" align="center" prop="commentId" v-if="false" />
      <el-table-column label="宠物名称" align="center" prop="recipeName" :show-overflow-tooltip="true" />
      <el-table-column label="评论内容" align="center" prop="content" :show-overflow-tooltip="true" />
      <el-table-column label="评分" align="center" prop="rating">
        <template #default="scope">
          <el-rate v-model="scope.row.rating" disabled show-score text-color="#ff9900" />
        </template>
      </el-table-column>
      <el-table-column label="评论用户" align="center" prop="userName" />
      <el-table-column label="评论时间" align="center" prop="createTime" width="180" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : scope.row.status === '1' ? 'warning' : 'danger'">
            {{ scope.row.status === '0' ? '正常' : scope.row.status === '1' ? '待审核' : '禁止显示' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="宠物名称" prop="recipeName">
          <el-input v-model="form.recipeName" disabled />
        </el-form-item>
        <el-form-item label="评论内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入评论内容" />
        </el-form-item>
        <el-form-item label="评分" prop="rating">
          <el-rate v-model="form.rating" :max="5" show-score />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusOptions" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listComment, getComment, delComment, updateComment } from '@/api/pet/comment'

const { proxy } = getCurrentInstance()

const commentList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const title = ref('')
const ids = ref([])
const single = ref(true)
const multiple = ref(true)

// 状态数据字典
const statusOptions = ref([
  { value: '0', label: '正常' },
  { value: '1', label: '待审核' },
  { value: '2', label: '禁止显示' }
])

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  recipeName: '',
  status: ''
})

// 表单参数
const form = ref({
  commentId: undefined,
  recipeId: undefined,
  recipeName: '',
  content: '',
  rating: 5,
  status: '0'
})

// 表单校验规则
const rules = ref({
  content: [{ required: true, message: '评论内容不能为空', trigger: 'blur' }],
  rating: [{ required: true, message: '评分不能为空', trigger: 'blur' }]
})

/** 查询评论列表 */
function getList() {
  loading.value = true
  listComment(queryParams.value).then(response => {
    commentList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    commentId: undefined,
    recipeId: undefined,
    recipeName: '',
    content: '',
    rating: 5,
    status: '0'
  }
  proxy.$refs['formRef']?.resetFields()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.$refs['queryForm']?.resetFields()
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.commentId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const commentId = row.commentId || ids.value[0]
  getComment(commentId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改评论'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['formRef']?.validate(valid => {
    if (valid) {
      updateComment(form.value).then(response => {
        proxy.$modal.msgSuccess('修改成功')
        open.value = false
        getList()
      })
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const commentIds = row.commentId || ids.value
  proxy.$modal
    .confirm('是否确认删除评论编号为"' + commentIds + '"的数据项？')
    .then(function () {
      return delComment(commentIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

// 页面加载时获取列表数据
onMounted(() => {
  getList()
})
</script>
